package com.rms.recruit_management_system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rms.recruit_management_system.pojo.Resume;
import com.rms.recruit_management_system.service.impl.ResumeServiceImpl;
import com.rms.recruit_management_system.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@Tag(name = "简历信息控制类", description = "用于对简历信息进行业务操作")
public class ResumeController {
    @Autowired
    private ResumeServiceImpl resumeService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/resume/list")
    @Operation(summary = "获取所有简历信息",description = "获取所有简历信息")
    public Result<IPage<Resume>> getResumeList(@RequestParam(defaultValue = "1") Integer current,
                                               @RequestParam(defaultValue = "10") Integer size,
                                               @RequestParam(defaultValue = "") String search){
        String key = "resume" + current + "_" + size + "_" + search;
        IPage<Resume> list=(IPage) redisTemplate.opsForValue().get(key);
        if(list!=null && list.getTotal()>0){
            new Result<>(200,"SUCCESS",list);
        }
        list=resumeService.getResumeList(current,size,search);
        redisTemplate.opsForValue().set(key,list,60, TimeUnit.SECONDS);
        return new Result<>(200,"SUCCESS",list);
    }

    @Operation(summary = "发送面试邀请",description = "发送面试邀请")
    @RequestMapping("/resume/sendInvite/{userId}")
    public Result<?> sendInvite(@PathVariable Integer userId){
        resumeService.sendInterviewInvitation(userId);
        cleanCache("resume_*");
        return new Result<>(200,"SUCCESS");
    }

    @Operation(summary = "删除简历信息",description = "根据id删除简历信息")
    @RequestMapping("/resume/delete/{id}")
    public Result<?> deleteResume(@PathVariable Integer id){
        resumeService.deleteResume(id);
        cleanCache("resume_*");
        return new Result<>(200,"SUCCESS");
    }

    @Operation(summary = "驳回简历申请",description = "驳回简历申请")
    @RequestMapping("/resume/reject/{userId}")
    public Result<?> rejectResume(@PathVariable Integer userId){
        resumeService.rejectResume(userId);
        cleanCache("resume_*");
        return new Result<>(200,"SUCCESS");
    }

    @Operation(summary = "获取待处理简历数量",description = "获取待处理简历数量")
    @RequestMapping("/resume/pendCount")
    public Result<?> getPendResumeCount(){
        Long count=resumeService.getPendResumeCount();
        return new Result<>(200,"SUCCESS",count);
    }
    @Operation(summary = "提交简历信息",description = "提交简历信息")
    @RequestMapping("/resume/submit")
    public Result<?> submitResume(@RequestBody Resume resume){
        resume.setSubmitTime(LocalDateTime.now());
        resumeService.addResume(resume);
        return new Result<>(200,"SUCCESS");
    }

    /**
     * 清空缓存
     * @param pattern
     */
    public void cleanCache(String pattern){
        Set keys=redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
